<?php

class FunctionsDao extends Dao {

    function __construct() {
        parent::__construct();
        $this->table = 'functions';
        $this->incrementing = false;
        $this->fillable = array('id','name','module_id','url','icon','sequence','is_enabled','is_show');
    }
    
    public function insertObject($obj){
        $result = DB::insert(DB::raw("INSERT INTO functions (id, name, module_id, url, icon, sequence, is_enabled, is_show, created_at) ".
                "VALUES ('".$obj->getId()."' ".
                        ", '".$obj->getName()."' ".
                        ", '".$obj->getModule()->getId()."' ".
                        ", '".$obj->getUrl()."' ".
                        ", '".$obj->getIcon()."' ".
                        ", '".$obj->getSequence()."' ".
                        ", '".$obj->getIsEnabled()."' ".
                        ", '".$obj->getIsShow()."' ".
                        ", null ".
                        ")"));
        if (!$result) { return null; }

        return $obj->getId();
    }

    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE functions SET id='".$obj->getId()."'"
                . ", name='".$obj->getName()."'"
                . ", module_id='".$obj->getModule()->getId()."'"
                . ", url='".$obj->getUrl()."'"
                . ", icon='".$obj->getIcon()."'"
                . ", sequence=".$obj->getSequence().""
                . ", is_enabled=".$obj->getIsEnabled().""
                . ", is_show=".$obj->getIsShow().""
                . ", updated_at=null"
                . " WHERE id='".$old_id."'"));
    
        return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM functions where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM functions where id='".$id."'"));

        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM functions"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    function updateModuleId($moduleId,$oldModuleId){
         try {
            DB::select( DB::raw("update functions set module_id = '$moduleId' where module_id = '$oldModuleId' ") );
            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function getListByModule($module_id){
        try {
            $result = parent::where('module_id','=',$module_id)->get();
            $list = array();
            foreach($result as $rowdata){
                $list[] = $this->toObject($rowdata);
            }
            return $list;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function toObject($rowdata=null) {
        $functions = new Functions();
        $functions->setId($rowdata->id);
        $functions->setName($rowdata->name);
            
            $module = new Module();
            $module->setId($rowdata->module_id);
            $module->setIsLoaded(false);
            $functions->setModule($module);
            
        $functions->setUrl($rowdata->url);
        $functions->setIcon($rowdata->icon);
        $functions->setSequence($rowdata->sequence);
        $functions->setIsEnabled($rowdata->is_enabled);
        $functions->setIsShow($rowdata->is_show);
        $functions->setIsLoaded(true);

        return $functions;
    }
    
}
